package com.feiyu.controller;


import com.feiyu.pojo.Emp;
import com.feiyu.service.EmpService;
import com.feiyu.util.JwtUtils;
import com.feiyu.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j //日志
@RestController
public class LoginController {

    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        log.info("登录" + emp);
        Emp empLogin = empService.login(emp);

        //生成JWT 令牌
        if (empLogin != null) {
            //设置内容
            Map<String, Object> claims = new HashMap<>();
            //设置内容  重点
            claims.put("id", empLogin.getId());
            claims.put("name", empLogin.getName());
            claims.put("username", empLogin.getUsername());
            String Jwt = JwtUtils.generateJwt(claims);
            return Result.success(Jwt);
        }


        return Result.error("用户名密码错误");

    }


}
